import Vue from "vue";
import store from "../store/index";
import VueRouter from "vue-router";

// 解决报错
const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;
// push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
	if (onResolve || onReject)
		return originalPush.call(this, location, onResolve, onReject);
	return originalPush.call(this, location).catch((err) => err);
};
// replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
	if (onResolve || onReject)
		return originalReplace.call(this, location, onResolve, onReject);
	return originalReplace.call(this, location).catch((err) => err);
};
Vue.use(VueRouter);


const login = () => import("@/views/account/login.vue");
// 官网页面1
const Index = () => import( /* webpackChunkName: "index" */ "@/views/Index.vue");
// const test = () => import(/* webpackChunkName: "test" */ "@/views/test/testTinymce.vue");
// const test = () =>
// 	import( /* webpackChunkName: "test" */ "@/views/test/testCkeditor.vue");

const modal_list = () => import("@/views/muban/modal-list.vue");
const add_modal = () => import("@/views/muban/add-modal.vue");


// 商品管理
const product_list = () => import("@/views/product/product-list.vue");
const product_form = () => import("@/views/product/product-form.vue");




const charge_form = () => import("@/views/charge/charge-form.vue");
const charge_list = () => import("@/views/charge/charge-list.vue");


//账号权限
const department_list = () => import("@/views/system/department-list.vue");
const job_list = () => import("@/views/system/job-list.vue");
const role_list = () => import("@/views/system/role-list.vue");
const menu_list = () => import("@/views/system/menu-list.vue");
const account_list = () => import("@/views/system/account-list.vue");



const routes = [
	// {
	// 	path: "/test",
	// 	name: "test",
	// 	component: test,
	// 	meta: {
	// 		title: "网站路由",
	// 		deep: 1,
	// 	},
	// },
	{
		path: "/login",
		name: "login",
		component: login,
		meta: {},
	},
	{
		path: "/",
		name: "index",
		component: modal_list,
		meta: {
			title: "模板",
			deep: 1,
		},
	},
	{
		path: "/add-modal",
		name: "add-modal",
		component: add_modal,
		meta: {
			root: "模板管理",
			title: "添加模板",
			deep: 2,
		},
	},
	{
		path: "/modal-list",
		name: "modal-list",
		component: modal_list,
		meta: {
			root: "模板管理",
			title: "模板列表",
			deep: 2,
		},
	},
	
	{
		path: "/charge-form",
		name: "charge-form",
		component: charge_form,
		meta: {
			root: "模板管理",
			title: "提交信息",
			deep: 2,
		},
	},
	
	{
		path: "/charge-list",
		name: "charge-list",
		component: charge_list,
		meta: {
			root: "模板管理",
			title: "提交列表",
			deep: 2,
		},
	},
	{
		path: "/product-list",
		name: "product-list",
		component: product_list,
		meta: {
			root: "商品管理",
			title: "商品列表",
			deep: 2,
		},
	},
	{
		path: "/product-form",
		name: "product-form",
		component: product_form,
		meta: {
			root: "商品管理",
			title: "基础商品编辑",
			deep: 2,
		},
	},

	{
		path: "/account-list",
		name: "account-list",
		component: account_list,
		meta: {
			root: "账号权限",
			title: "子账号管理",
			deep: 2,
		},
	},
	{
		path: "/department-list",
		name: "department-list",
		component: department_list,
		meta: {
			root: "账号权限",
			title: "部门管理",
			deep: 2,
		},
	},
	{
		path: "/job-list",
		name: "job-list",
		component: job_list,
		meta: {
			root: "账号权限",
			title: "岗位管理",
			deep: 2,
		},
	},
	{
		path: "/menu-list",
		name: "menu-list",
		component: menu_list,
		meta: {
			root: "账号权限",
			title: "菜单管理",
			deep: 2,
		},
	},
	{
		path: "/role-list",
		name: "role-list",
		component: role_list,
		meta: {
			root: "账号权限",
			title: "角色管理",
			deep: 2,
		},
	},



	

	{
		path: "*",
		redirect: "/",
	},
];

const scrollBehavior = (to, from, savedPosition) => {
	// console.log('滚动行为', to, from, savedPosition)
	return {
		x: 0,
		y: 0
	};
};

const router = new VueRouter({
	routes,
	// base: "/",
	base: "/admin",
	// base: "/admin",
	mode: "history",
	scrollBehavior,
});

router.beforeEach((to, from, next) => {
	let token = localStorage.getItem("token");
	let userId = localStorage.getItem("userId");
	let is_login_curr = token && userId;

	// debugger
	if (!is_login_curr && to.meta.requireAuth) {
		// debugger
		alertErr("请先登录");
		next("/login");
	} else {
		next();
	}
});

router.afterEach((to, from) => {
	// console.log("切换导航, 重新配置微信分享");
	// wxShare.getSign();
});

export default router;